package com.xfhy.wallpaperdemo.account;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.PeriodicSync;
import android.os.Build;
import android.os.Bundle;

import com.xfhy.wallpaperdemo.DaemonLog;
import com.xfhy.wallpaperdemo.live.nk;

import java.util.List;

/**
 * @author : xfhy
 * Create time : 2020/11/2 17:17
 * Description :
 */
public class AccountHelper {
    public static String a() {
        return "com_dn_cleanmaster";
    }

    public static void autoSyncAccount(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        DaemonLog.d("SyncManager autoSyncAccount,accountManager=" + accountManager);
        if (accountManager != null) {
            String a = a();
            String b = b();
            Account account = new Account(a, b);
            String a2 = a(context);
            DaemonLog.d("SyncManager autoSyncAccount,accountName=" + a + ",accountType=" + b);
            try {
                if (accountManager.getAccountsByType(b()).length <= 0) {
                    accountManager.addAccountExplicitly(account, (String) null, Bundle.EMPTY);
                    DaemonLog.d("add account success");
                    ContentResolver.setIsSyncable(account, a2, 1);
                    ContentResolver.setSyncAutomatically(account, a2, true);
                    ContentResolver.setMasterSyncAutomatically(true);
                }
                ContentResolver.removePeriodicSync(account, a2, Bundle.EMPTY);
                ContentResolver.addPeriodicSync(account, a2, Bundle.EMPTY, Build.VERSION.SDK_INT >= 24 ? 900 : 3600);
                requestSync(context, account, false);
            } catch (Exception e) {
                DaemonLog.e("autoSyncAccount error", e);
            }
        }
    }

    public static String b() {
        return "com.dn.account";
    }

    public static void cancelSync(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        DaemonLog.d("SyncManager cancelSync,accountManager=" + accountManager);
        if (accountManager != null) {
            String a = a();
            String b = b();
            Account account = new Account(a, b);
            String a2 = a(context);
            DaemonLog.d("SyncManager cancelSync,accountName=" + a + ",accountType=" + b);
            try {
                if (Build.VERSION.SDK_INT >= 22) {
                    accountManager.removeAccountExplicitly(account);
                }
            } catch (Exception e) {
                DaemonLog.e("removeAccountExplicitly error", e);
            }
            try {
                ContentResolver.removePeriodicSync(account, a2, Bundle.EMPTY);
            } catch (Exception e2) {
                DaemonLog.e("cancelSync error", e2);
            }
        }
    }

    public static void reSync(Context context) {
        List<PeriodicSync> periodicSyncs;
        Account account = new Account(a(), b());
        String a = a(context);
        do {
            ContentResolver.removePeriodicSync(account, a, Bundle.EMPTY);
            periodicSyncs = ContentResolver.getPeriodicSyncs(account, a);
            if (periodicSyncs == null || periodicSyncs.isEmpty()) {
                DaemonLog.d("jobs empty");
            }
            ContentResolver.removePeriodicSync(account, a, Bundle.EMPTY);
            periodicSyncs = ContentResolver.getPeriodicSyncs(account, a);
            break;
        } while (periodicSyncs.isEmpty());
        DaemonLog.d("jobs empty");
        ContentResolver.addPeriodicSync(account, a, Bundle.EMPTY, Build.VERSION.SDK_INT >= 24 ? 900 : 3600);
        requestSync(context, account, false);
    }

    public static void requestSync(Context context, Account account, boolean z2) {
        try {
            Bundle bundle = new Bundle();
            bundle.putBoolean("force", true);
            bundle.putBoolean("expedited", true);
            if (z2) {
                bundle.putBoolean("require_charging", false);
            }
            ContentResolver.requestSync(account, a(context), bundle);
        } catch (Exception e) {
            DaemonLog.e("requestSync error", e);
        }
    }

    public static String a(Context context) {
        return context.getPackageName() + ".provider";
    }
}

